package debux.apps.mltr.model;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

public class DatabaseController {
	static Context context;
	static MLTRDatabaseAdapter dbAdapter;
	public DatabaseController(){
		
	}
	
	public static void init(Context _context){
		context  = _context;
		dbAdapter = new MLTRDatabaseAdapter(_context);
		dbAdapter.open();
	}
	
	public void close(){
		dbAdapter.close();
	}
	
	public static void getAllAlbums(ArrayList<AlbumInfo> albumsList){
		if (albumsList==null){
			return;
		}
		albumsList.clear();
		Cursor cursor = dbAdapter.getAllAlbums();
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			String genre = cursor.getString(2);
			String released = cursor.getString(3);
			String label = cursor.getString(4);
			String link = cursor.getString(5);
			AlbumInfo albumInfo = new AlbumInfo(id, name, genre, released, label, link);
			albumsList.add(albumInfo);
			cursor.moveToNext();
		}
		cursor.close();
	}
	public static void getAllSongs(ArrayList<SongInfo> songsList){
		if (songsList==null){
			return;
		}
		songsList.clear();
		Cursor cursor = dbAdapter.getAllSongs();
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			int id = cursor.getInt(0);
			int albumId = cursor.getInt(1);
			String name = cursor.getString(2);
			String genre = cursor.getString(3);
			String length = cursor.getString(4);
			int orderId = cursor.getInt(5);
			String link = cursor.getString(6);
			String lyrics = cursor.getString(7);
			SongInfo songInfo = new SongInfo(id, albumId, name, genre, length, orderId, link,lyrics);
			songsList.add(songInfo);
			cursor.moveToNext();
		}
		cursor.close();
	}
	public static void getAllSongsByAlbumId(int album_id, ArrayList<SongInfo> songsList){
		if (songsList==null){
			return;
		}
		songsList.clear();
		Cursor cursor = dbAdapter.getAllSongsByAlbumId(album_id);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			int id = cursor.getInt(0);
			int albumId = cursor.getInt(1);
			String name = cursor.getString(2);
			String genre = cursor.getString(3);
			String length = cursor.getString(4);
			int orderId = cursor.getInt(5);
			String link = cursor.getString(6);
			String lyrics = cursor.getString(7);
			SongInfo songInfo = new SongInfo(id, albumId, name, genre, length, orderId, link,lyrics);
			songsList.add(songInfo);
			cursor.moveToNext();
		}
		cursor.close();
	}
	
	public static void searchAllSongsByName(String songName, ArrayList<SongInfo> songsList){
		if (songsList==null){
			return;
		}
		songsList.clear();
		Cursor cursor = dbAdapter.searchAllSongsByName(songName);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			int id = cursor.getInt(0);
			int albumId = cursor.getInt(1);
			String name = cursor.getString(2);
			String genre = cursor.getString(3);
			String length = cursor.getString(4);
			int orderId = cursor.getInt(5);
			String link = cursor.getString(6);
			String lyrics = cursor.getString(7);
			SongInfo songInfo = new SongInfo(id, albumId, name, genre, length, orderId, link,lyrics);
			songsList.add(songInfo);
			cursor.moveToNext();
		}
		cursor.close();
	}	
}
